<?php
/**
   *  Base class for working with SQL-Servers
 */
class DataBase
{
	var $m_hostID; //указатель на соединение с БД MySQL
	var $m_lastError;
	var $m_db_host;
	var $m_db_user;
	var $m_db_pass;
	var $m_db_name;
	var $m_use_persistent;
	var $m_fetch_mode = MYSQL_BOTH;

	function DataBase()
	{
		$this->m_db_host = DB_SERVER;
		$this->m_db_user = DB_USER;
		$this->m_db_pass = DB_PASSWORD;
		$this->m_db_name   = DB_DATABASE;
		$this->m_use_persistent = DB_PERSISTENT; //use persistent connections. You can read about it here -  http://www.ringo-web.ru/docs/persistent-connections.html

		$this->Connect();
	}


	function Connect()
	{
		if ($this->m_use_persistent)
		{
			$this->m_hostID = mysql_pconnect($this->m_db_host, $this->m_db_user, $this->m_db_pass);
		}
		else
		{
			$this->m_hostID = mysql_connect($this->m_db_host, $this->m_db_user, $this->m_db_pass);
		}

		if ($this->m_hostID)
		{
			mysql_select_db($this->m_db_name, $this->m_hostID);

		}
		mysql_query("SET CHARACTER SET `".DATABASE_CHARSET."`");
	}


	function Query($query,$unbuffered=false)
	{
		//$query=mysql_real_escape_string($query);
		if (!$this->m_hostID)
		{
			$this->m_lastError="Connection hasn't been opened yet!";
		}
		else
		{
			if ($unbuffered)
			{
				$qID=mysql_unbuffered_query($query);

			}
			{
				//mysql_query("SET NAMES cp1251");
				$qID=mysql_query($query);
				//print_r($query); echo "<br>";

			}
			if ($qID)
			{
				return $qID;
			}
			else
			{
				$this->m_lastError=mysql_error();
				if (SHOW_ERROR_QUERY)
				{
					print_r($query);
					print_r($this->m_lastError);
				}
			}
		}
	}


	function Disconnect()
	{
		if (!$this->m_hostID)
		{
			$this->m_lastError="Connection hasn't been opened yet!";
		}
		else
		{
			if (!$this->m_use_persistent)
			{
				if (!mysql_close($this->m_hostID))
				{
					$this->m_lastError="Can't Close Connection";
				}
			}
		}

	}

	function FetchArray($qID)
	{
		return @mysql_fetch_array($qID, $this->m_fetch_mode);
	}

	function SetMode($assoc = true, $num = true)
	{
		if ($assoc)
		{
			if ($num)
			{
				$this->m_fetch_mode = MYSQL_BOTH;
			}
			else
			{
				$this->m_fetch_mode = MYSQL_ASSOC;
			}
		}
		else
		{
			if ($num)
			{
				$this->m_fetch_mode = MYSQL_NUM;
			}
			else
			{
				trigger_error('CARXSQL->SetMode() error: no return types defined');
			}
		}
	}
}
?>